#!/bin/csh
#  Copyright (C) 2002 Regents of the University of Michigan, portions used with permission 
#  For more information, see http://csem.engin.umich.edu/tools/swmf
# pTEC: Post process output files for use in tecplot
#    - combine all *.tec files into one file per iteration
#    - run preplot to make a tecplot binary file and remove intermediate files.
#

# Help
if ( $1 == 'help' || $1 == '-h' || $1 == '-help' || $1 == '--help' ) then
   echo "Usage:"
   echo ""
   echo "pTEC [p|g] [r,b=BASENAME]"
   echo ""
   echo "The order of flags does not matter.  A maximum of 6 flags can be used.       "
   echo ""
   echo "- If 'p' and preplot is available in the PATH then it will also be run.      "
   echo "     - If 'r' is also specified, the .dat file will be deleted after preplot."
   echo "- If 'g' is specified, the .dat file will be gzip'd (independant of 'p')     "
   echo "- If 'b=BASENAME' then only files starting with the path BASENAME are        "
   echo "     processed.                                                              "
   echo "     - To process all files in the IO2 directory use 'b=IO2/'   not 'b=IO2'  "
   echo ""
   echo "Examples:"
   echo ""
   echo "pTEC"
   echo "pTEC p"
   echo "pTEC p r"
   echo "pTEC g"
   echo "pTEC b=IO2/"
   echo "pTEC p r b=IO2/y=0"
   echo ""
   exit
endif

# Setup which portions of script should be run

set Preplot=0
if ( "$1" == "p" || \
     "$2" == "p" || \
     "$3" == "p" || \
     "$4" == "p" || \
     "$5" == "p" || \
     "$6" == "p"     ) then
  set Preplot=1
endif

set Remove=0
if ( "$1" == "r" || \
     "$2" == "r" || \
     "$3" == "r" || \
     "$4" == "r" || \
     "$5" == "r" || \
     "$6" == "r"     ) then
  set Remove=1
endif

set GZIP=0
if ( "$1" == "g" || \
     "$2" == "g" || \
     "$3" == "g" || \
     "$4" == "g" || \
     "$5" == "g" || \
     "$6" == "g"     ) then
  set GZIP=1
endif

# if b=PATTERN is used to set the directory and files to process
# then overwrite the default BASENAME
set BASENAME = './IO2/'

if ( "$1" =~ b=* ) set BASENAME = `echo $1 | sed s/b=//`
if ( "$2" =~ b=* ) set BASENAME = `echo $2 | sed s/b=//`
if ( "$3" =~ b=* ) set BASENAME = `echo $3 | sed s/b=//`
if ( "$4" =~ b=* ) set BASENAME = `echo $4 | sed s/b=//`
if ( "$5" =~ b=* ) set BASENAME = `echo $5 | sed s/b=//`
if ( "$6" =~ b=* ) set BASENAME = `echo $6 | sed s/b=//`

# set the current path location so that we can return here
set dd = `pwd`

# Make sure that we can overwrite any existing files
unset noclobber

# Check to make sure that preplot can be found in the path somewhere
set ppFound=0
if ( $Preplot == 1) then
   (which preplot | grep -v 'Command not found' > .tmp1) >&/dev/null
   if (!(-z .tmp1)) then
      echo "--Using preplot from "`cat .tmp1`
      set ppFound=1
   else
      echo "Cannot find preplot in path, ignoring 'p' option"
   endif
   rm -f .tmp1
endif

############################################### IO2 begin ###########

# Process BASENAME to get directory part and filename part.
set BASENAME=${BASENAME}.tmp
set d  = `dirname ${BASENAME}`
set ff = `basename ${BASENAME} .tmp`
if (${ff} == .tmp) then
   set ff = './'
endif

echo '========================================================================'
echo 'Beginning cat of .tec files ...                                         '
echo '--Working in directory: '${d}'/   on files: '${ff}'*.T '${ff}'*.tec'

# Go to the directory where the run is supposed to be processed and
# proceed with processing the files
cd ${d} || echo "pTec could not cd ${d}" && exit 1;
touch ${ff}TMP.T
foreach i (${ff}*.T)
    if (!( ${i} == ${ff}TMP.T )) then
        set f = `basename $i .T`
        echo '   working on '${f}' ...'
        cat ${f}_*.tec > ${f}.dat
	sleep 1
        rm -f $i
        rm -f ${f}_*.tec
    endif
end
rm -f ${ff}TMP.T

# preplot files
if ( $ppFound == 1 ) then
    echo 'Beginning preplot ...'
    echo '--Working in directory: '${d}'/   on files: '${ff}'*.dat'
    touch ${ff}TMP.dat
    foreach i (${ff}*.dat)
    if (!( ${i} == ${ff}TMP.dat )) then
        set f = `basename $i .dat`
        echo '   preplotting '${i}' ...'
        preplot ${i}
	if (-f ${f}.plt) then
            if ($GZIP == 1) then
                gzip -f ${i}
	    else
                if ( $Remove == 1 ) then
                    rm -f ${i}
                endif
            endif
        else
            echo ' WARNING WARNING WARNING, .plt file not created.'
        endif
    endif
    end
    rm -f ${ff}TMP.dat
else
    if ($GZIP == 1) then
        echo 'Beginning gzip ...'
        echo '--Working in directory: '${d}'/   on files: '${ff}'*.dat'
        gzip -f ${ff}*dat
    endif
endif

echo '========================================================================'

cd ${dd} || echo "pTec could not cd ${dd}" && exit 1;
############################################### IO2 end #############

# preplot or gzip files
if ( $ppFound == 1 ) then
    echo 'Beginning preplot ...'
    echo '--Working in directory: '${d}'/   on files: '${ff}'*.dat'
    touch ${ff}TMP.dat
    foreach i (${ff}*.dat)
    if (!( ${i} == ${ff}TMP.dat )) then
        set f = `basename $i .dat`
        echo '   preplotting '${i}' ...'
        preplot ${i}
	if (-f ${f}.plt) then
            if ($GZIP == 1) then
                gzip -f ${i}
            else
                if ( $Remove == 1 ) then
                    rm -f ${i}
                endif
            endif
        else
            echo ' WARNING WARNING WARNING, .plt file not created.'
        endif
    endif
    end
    rm -f ${ff}TMP.dat
else
    if ($GZIP == 1) then
        echo 'Beginning gzip ...'
        echo '--Working in directory: '${d}'/   on files: '${ff}'*.dat'
        gzip -f ${ff}*dat
    endif
endif

echo '========================================================================'

cd ${dd} || echo "pTec could not cd ${dd}" && exit 1;
endif
############################################### spherical end #######
